from django.http import HttpResponse, JsonResponse
from weather.utils.Predictor import Predictor
from weather.models import Day

# Create your views here.

DEFAULT_UID = 1
DEFAULT_NUMS = 5

predictor = Predictor()
wind_bearing_arr = [
    '正北',
    '东北',
    '正东',
    '东南',
    '正南',
    '西南',
    '正西',
    '西北'
]


def predict(request):
    uid = DEFAULT_UID
    nums = DEFAULT_NUMS
    if request.method == 'GET':
        uid = request.GET.get('stdNum')
        nums = request.GET.get('nums')
        print(request)
    elif request.method == 'POST':
        uid = request.POST['stdNum']
        nums = request.POST['nums']
    else:
        return HttpResponse('Please use get or post')
    res = predictor.pred_n(int(uid), int(nums))
    res_data = {'pre': [], 'is_test': False}
    for id in res:
        this_day = Day.objects.get(item_id=id)
        res_item = {'item_id': id, 'temperature': this_day.temperature, 'humidity': this_day.humidity,
                    'wind_speed': this_day.wind_speed, 'visibility': this_day.visibility, 'pressure': this_day.pressure,
                    'wind_bearing': wind_bearing_arr[this_day.wind_bearing]}
        res_data['pre'].append(res_item)

    return JsonResponse(res_data)


def tst_uid(request):
    # for test
    uid = DEFAULT_UID
    nums = DEFAULT_NUMS
    if request.method == 'GET':
        uid = request.GET.get('stdNum')
        nums = request.GET.get('nums')
        print(request)
    elif request.method == 'POST':
        uid = request.POST.get('stdNum')
        nums = request.POST.get('nums')
    else:
        return HttpResponse('Please use get or post')
    print("uid:{}  nums:{}".format(uid,nums))
    res = predictor.pred_n(int(uid), int(nums))
    res_data = {'pre': [], 'is_test': True}
    for id in res:
        this_day = Day.objects.get(item_id=id)
        res_item = {'item_id': id, 'temperature': this_day.temperature, 'humidity': this_day.humidity,
                    'wind_speed': this_day.wind_speed, 'visibility': this_day.visibility, 'pressure': this_day.pressure,
                    'wind_bearing': wind_bearing_arr[this_day.wind_bearing]}
        res_data['pre'].append(res_item)

    return JsonResponse(res_data)


def tget(request):
    # for test an uid
    uid = DEFAULT_UID
    nums = DEFAULT_NUMS

    res = predictor.pred_n(int(uid), int(nums))
    res_data = {'pre': [], 'is_test': True}
    for id in res:
        this_day = Day.objects.get(item_id=id)
        res_item = {'item_id': id, 'temperature': this_day.temperature, 'humidity': this_day.humidity,
                    'wind_speed': this_day.wind_speed, 'visibility': this_day.visibility, 'pressure': this_day.pressure,
                    'wind_bearing': wind_bearing_arr[this_day.wind_bearing]}
        res_data['pre'].append(res_item)

    return JsonResponse(res_data)